<?php
declare (strict_types = 1);

namespace app\api\dao;

use app\api\model\User;

class UserDao
{
    //用户创建
    public static function createUser($request): array
    {
        $user = new User();
        $user->user_id = date('ymdHis') . mt_rand(100000, 999999);
        $user->avatar_url = $params['avatar_url'] ?? self::getImg();
        $user->nick_name = $request->param('nick_name', '');
        $user->email = $request->param('email', '');
        $user->gender = $request->param('gender', '');
        $user->country = $request->param('country', '');
        $user->province = $request->param('province', '');
        $user->city = $request->param('city', '');
        $user->secret = $params['secret'];
        $user->token = uuid();

        //邮箱去重复验证
        if(self::getUserByEmail($user->email))
        {
            return ["status"=>false,"msg"=>"邮箱已注册"];
        }else{
            $user->save();
            return ["status"=>true,"msg"=>"注册成功","data"=>$user];
        }


    }

    //用户鉴权登录
    public static function login($request): array
    {
        $user = self::getUserByEmail($request->param('email'));
        if($user)
        {
            if($user->secret === $request->param('secret'))
            {
                $user->token = uuid();
                $user->update_time = date("Y-m-d H:i:s");
                $user->save();
                return ["status"=>true,"msg"=>"登录成功","data"=>$user];
            }else{
                return ["status"=>false,"msg"=>"邮箱密码不匹配"];
            }
        }else{
            $user->save();
            return ["status"=>false,"msg"=>"邮箱未注册"];
        }


    }

    //退出登录
    public static function loginOut($request): array
    {
        $user = self::getUserByToken($request->header('token'));
        if($user)
        {
            $user->token = "";
            $user->update_time = date("Y-m-d H:i:s");
            $user->save();
            return ["status"=>true,"msg"=>"登录退出成功"];
        }else{
            $user->save();
            return ["status"=>false,"msg"=>"您还未登录"];
        }


    }


    //忘记密码
    public static function forget($request): array
    {
        $user = self::getUserByEmail($request->param('email'));
        if ($user) {
            //密码重置 邮箱发送更改后的密码 OE+用户ID后两位10+时间日期0714+!@#

        } else {
            $user->save();
            return ["status" => false, "msg" => "邮箱未注册"];
        }
    }





    /**
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public static function getUserByUserId($uid)
    {
        return User::where('user_id', $uid)
            ->where('is_delete', 0)
            ->find();
    }

    /**
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\DataNotFoundException
     */
    public static function getUserByEmail($email)
    {
        return User::where('email', $email)
            ->where('is_delete', 0)
            ->find();
    }


    public static function getUserByToken($token)
    {
        return User::where('token', $token)
            ->where('is_delete', 0)
            ->find();
    }



    public static function getImg(): string
    {
        $imgUrl = [
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/2f281650b28076d0.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/3eb1ac20a2c208e0.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/9e91f7e7aa3f1a25.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/f0153fd2fba082b4.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/51f66c686aeab9de.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/868f7fa36065781e.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/09f453c1d1cb54a9.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/eb93c514e66eb1df.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/2d4633429156b06e.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/153b79856ca155ab.jpg',
            'https://gaokao-1254083922.cos.ap-nanjing.myqcloud.com/school_logo/39cf8a36464dcae3.jpg',
        ];
        $key = rand(0, count($imgUrl) - 1);
        return $imgUrl[$key];
    }

}
